package managementSystem;

import nature.People;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.function.BiFunction;

/**
 * @学生类
 */
public class Student extends People {
    private String studentId;                   //学号
    private String college;                     //学院
    private String major;                       //专业
    private String classes;                     //班级
    private String headTeacher;                 //班主任
    private HashMap<String, Number> grades;     //成绩
    private People people = new People();       //个人信息

    public Student(){}
    public Student(HashMap<String, Object> studentInfo) throws NoSuchFieldException, IllegalAccessException {
        this.set(studentInfo);
    }

    /**
     * 获取学生信息方法
     * @return  学生信息HashMap
     * @throws IllegalAccessException
     */
    @Override
    public HashMap<String, Object> get() throws IllegalAccessException {
        //获取学生信息
        HashMap<String, Object> studentInfo = tool.GetAndSetField.getFieldValue(this);
        //获取个人信息
        studentInfo.putAll(tool.GetAndSetField.getFieldValue(this.people));
        //返回数据
        return studentInfo;
    }

    /**
     * 获取学生信息方法
     * @param getFields 获取属性名
     * @return  学生信息HashMap
     * @throws IllegalAccessException
     */
    @Override
    public HashMap<String, Object> get(String[] getFields) throws IllegalAccessException {
        //获取学生信息
        HashMap<String, Object> studentInfo = tool.GetAndSetField.getFieldValue(this, getFields);
        //获取个人信息
        studentInfo.putAll(tool.GetAndSetField.getFieldValue(this.people, getFields));
        //返回数据
        return studentInfo;
    }

    /**
     * 设置学生信息方法
     * @param studentInfo 个人信息HashMap
     * @return  设置成功属性
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @Override
    public ArrayList<String> set(HashMap<String, Object> studentInfo) throws NoSuchFieldException, IllegalAccessException {
        ArrayList<String> setSuccessFields = this.people.set(studentInfo);              //设置个人信息
        BiFunction<String, Object, Boolean> biFunction = (fieldName, fieldValue) -> {   //检查函数
            HashMap<String, String> check = new HashMap<>() {{                          //信息检查
                put("major", "\\S+");
                put("classes", "\\d{4}");
                put("headTeacher", "\\S+");
                put("studentId", "\\d{12}");
                put("college", "(电气与信息工程学院|纺织服装学院|计算机与通信学院|经济学院|外国语学院|设计艺术学院|机械工程学院|材料与化工学院|管理学院|计算科学与电子学院|建筑工程学院|建筑工程学院|应用技术学院)");
            }};
            //获取正则
            String pattern = check.get(fieldName);
            //返回检查结果
            return (fieldName.equals("grades") && fieldValue instanceof HashMap)
                    || (pattern != null && fieldValue instanceof String && ((String) fieldValue).matches(pattern));
        };
        //设置学生信息
        setSuccessFields.addAll(tool.GetAndSetField.setFieldValue(this, studentInfo, biFunction));
        //返回数据
        return setSuccessFields;
    }
}